package pl.edu.agh.sius.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import pl.edu.agh.sius.gui.model.FilesTableModel;
import pl.edu.agh.sius.gui.model.ServersTableModel;
import pl.edu.agh.sius.schema.simplep2pserver.FileInfo;

public class DownloadJPanel extends JPanel{
	private static final long serialVersionUID = 4209897630008326475L;
	
	private ServersTableModel serversTableModel;	
	private FilesTableModel filesTableModel;
	
	private FileInfo selectedFile;
	private String selectedServer;
	
	public void init(){
		setLayout(new GridBagLayout());
		
		JLabel serversTableTitle = new JLabel("Servers list");
    	JScrollPane serversTable = createServersTable();
    	JLabel filesTableTitle = new JLabel("Files list");
    	JScrollPane filesTable = createFilesTable();
    	JButton downloadButton = new JButton("Download file");
    	JButton refreshButton = new JButton("Refresh servers list");
    	
    	GridBagConstraints c = new GridBagConstraints();
    	c.fill = GridBagConstraints.HORIZONTAL;
    	c.insets = new Insets(0, 10, 0, 10);
    	c.anchor = GridBagConstraints.NORTH;
    	c.weightx = 0.5;
    	c.weighty = 0.5;
    	
    	c.gridy = 0;
    	c.gridx = 0;
    	this.add(serversTableTitle, c);
    	
    	c.gridx = 1;
    	this.add(filesTableTitle, c);
     	
    	c.gridy = 1;
    	c.gridx = 0;
    	this.add(serversTable, c);
    	
    	c.gridx = 1;
    	this.add(filesTable, c);
    	
    	c.gridx = 0;
    	c.gridy = 2;
    	
    	this.add(refreshButton, c);
    	
      	refreshButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		serversTableModel.refreshServersList();
        	}
        });
    	
    	c.gridx = 1;
    	c.gridy = 2;
    	this.add(downloadButton, c);
    	
    	downloadButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		filesTableModel.downloadFile(selectedServer, selectedFile);
        	}
        });
    	

	}
	
	public void refresh() {
		serversTableModel.refreshServersList();
	}
	
	private JScrollPane createServersTable(){
		serversTableModel.refreshServersList();
		JTable serversTable = new JTable(serversTableModel);
		serversTable.setFillsViewportHeight(true);
		
		serversTable.addMouseListener(new MouseAdapter() {
			@SuppressWarnings("unchecked")
			public void mouseClicked(MouseEvent e) {
				if( javax.swing.SwingUtilities.isLeftMouseButton(e) ){
					int row = ((JTable)e.getSource()).getSelectedRow();
					
					selectedServer = (String) serversTableModel.getData(row);
					filesTableModel.setData((List<Object>)(List<?>)filesTableModel.getFiles(selectedServer));
				}
			}
		});
		
		JScrollPane serversTablePanel = new JScrollPane(serversTable);
		serversTablePanel.setPreferredSize(new Dimension(200, 200));
		
		return serversTablePanel;
	}
	
	private JScrollPane createFilesTable(){	
		JTable filesTable = new JTable(filesTableModel);
		filesTable.setFillsViewportHeight(true);
		
		filesTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if( javax.swing.SwingUtilities.isLeftMouseButton(e) ){
					int row = ((JTable)e.getSource()).getSelectedRow();
					selectedFile = (FileInfo)filesTableModel.getData(row);
				}
			}
		});
		
		JScrollPane filesTablePanel = new JScrollPane(filesTable);
		filesTablePanel.setPreferredSize(new Dimension(200, 200));
		
		return filesTablePanel;
	}
	
	public ServersTableModel getServersTableModel() {
		return serversTableModel;
	}

	public void setServersTableModel(ServersTableModel serversTableModel) {
		this.serversTableModel = serversTableModel;
	}

	public FilesTableModel getFilesTableModel() {
		return filesTableModel;
	}

	public void setFilesTableModel(FilesTableModel filesTableModel) {
		this.filesTableModel = filesTableModel;
	}
	
	
}
